﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Contacts;
using System.Security.Principal;

namespace Contacts
{
    public partial class compare : Form
    {
        Form currentform = Form.ActiveForm;
        DataSet chgdata;
        DataSet dscont;
        DataTable chgtable = new DataTable();
        DataRow[] rc;
        DataRow nra;
        DataRow nrb;
        DataRow nrc;
        DataRow chgrowa;
        DataRow chgrowb;
        DataRow chgrowc;
        DataRow originalrow;
        DataRow currentrownew;
        bool typ;
        display nm;
        FormCollection forms = Application.OpenForms;
        string timeofcurrent;
        string[] t;
        string button;
        string[] contact = new string[]{"contact_id","first_name","middle_name","last_name","job_title",  
            "company_id","direct_phone","mobile_phone","email","timestamp","user"};
        string[] company = new string[]{"company_id","company_name","street","suite","city","state","zip",
            "main_phone","fax","category_id", "timestamp", "user"};
        string[] conlab = new string[] {"Record:","First Name:","Middle Init:","Last Name:","Job Title:",
            "Company:","Direct:","Mobile:","Email:","Timestamp:","User:"};
        string[] comlab = new string[] {"Record:","Company Name:","Street:","Suite:","City:","State:",
            "Zip:","Main:","Fax:","Category:","TimeStamp:","User:"};
        object[] labels;
        object[] pulledcollection;
        object[] currentcollection;
        object[] usercollection;
        
        public compare(DataRow pulledrow, DataRow currentrow, DataSet contacts, string timepull, DataSet changeddata)
        {
            InitializeComponent();

            timeofcurrent = currentrow["timestamp"].ToString();
            t = timeofcurrent.Split(new Char[] { ' ' });
            originalrow = pulledrow;
            currentrownew = currentrow;
            Type type = originalrow.ItemArray[5].GetType();
            typ = (type.Name == "Int32");
                    
            dscont = contacts;
            labels = new object[] {lab_0,lab_1,lab_2,lab_3,lab_4,lab_5,lab_6,
                lab_7,lab_8,lab_9,lab_10,lab_11};
            rc = new DataRow[] {pulledrow, currentrow, pulledrow};
            pulledcollection = new object[] {tbo_0_pull, tbo_1_pull, tbo_2_pull, tbo_3_pull, tbo_4_pull, tbo_5_pull,
             tbo_6_pull, tbo_7_pull, tbo_8_pull, tbo_9_pull, tbo_10_pull, tbo_11_pull};
            currentcollection = new object[] {tbo_0_curr, tbo_1_curr, tbo_2_curr, tbo_3_curr, tbo_4_curr, tbo_5_curr,
                tbo_6_curr, tbo_7_curr, tbo_8_curr, tbo_9_curr, tbo_10_curr, tbo_11_curr};
            usercollection = new object[] {tbo_0_user, tbo_1_user, tbo_2_user, tbo_3_user, tbo_4_user, tbo_5_user,
                tbo_6_user, tbo_7_user, tbo_8_user, tbo_9_user, tbo_10_user, tbo_11_user};
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Interval = 5000;
            timer1.Enabled = true;
            //timer1.Start();
            try
            {
                nm = new display();
                nm.Visible = false;
                nm.BackColor = Color.Khaki;
                nm.ShowDialog();
            }
            catch
            {

            }
            chgdata = changeddata;            
            
            string username = System.Environment.UserName;
            int c = 0;
            int u = 0;
                 
            lab_dp.Text = "You pulled data @ " + timepull + "";
            lab_cr.Text = "" + username + " updated \nhighlighted fields @ " + t[1] + " " + t[2] + "";
            if (typ == true)
            {
                lab_11.Visible = false;
                tbo_11_pull.Visible = false;
                tbo_11_curr.Visible = false;
                tbo_11_user.Visible = false;                              
            }
            columnsadd();
            nra = chgtable.NewRow();
            labelname();
            nra = chgtable.NewRow();
            nrb = chgtable.NewRow();
            nrc = chgtable.NewRow();
            fill();
            but_b.Select();
            
        }        

        private void columnsadd()
        {
            int cc;

            if (typ == true)
            {cc = contact.Length;}
            else
            {cc = company.Length;}            
            for (int i = 0; i < cc; i++)
            {
                switch (cc)
                {
                    case 11:
                        {
                            if (contact[i] == "contact_id" || contact[i] == "company_id")
                            { chgtable.Columns.Add(contact[i], Type.GetType("System.Int32")); }
                            else
                            { chgtable.Columns.Add(contact[i]); }
                        } break;
                    case 12:
                        {
                            if (company[i] == "company_id" || company[i] == "category_id")
                            { chgtable.Columns.Add(company[i], Type.GetType("System.Int32")); }
                            else
                            { chgtable.Columns.Add(company[i]); }
                        } break;
                }            
            }            
        }

        private void labelname()
        {
            int lc;
            if (typ == true)
            { lc = contact.Length; }
            else
            { lc = company.Length; }  
            
            for (int i = 0; i < lc; i++)
            {
                Control l = (Control)labels[i];
                if (lc == 11)
                { l.Text = conlab[i]; }
                else
                { l.Text = comlab[i]; }
            }
        }

        private void fill()
        {
            try
            {
                for (int r = 0; r < 3; r++)
                {
                    int tbc;
                    if (typ == true)
                    { tbc = 11; }
                    else
                    { tbc = 12; }

                    for (int i = 0; i < tbc; i++)
                    {
                        if (r == 1)
                        {
                            switch (typ)
                            {
                                case true:
                                    {
                                        nra[contact[i]] = currentrownew[contact[i]];
                                        nrb[contact[i]] = currentrownew[contact[i]];
                                        nrc[contact[i]] = currentrownew[contact[i]];
                                    } break;
                                case false:
                                    {
                                        nra[company[i]] = currentrownew[company[i]];
                                        nrb[company[i]] = currentrownew[company[i]];
                                        nrc[company[i]] = currentrownew[company[i]];
                                    } break;
                            }
                        }
                        if (i == 5 && typ == true)
                        {
                            switch (r)
                            {
                                case 0:
                                    {
                                        Control tb = (Control)pulledcollection[i];
                                        int contid = (int)rc[r][5, DataRowVersion.Original];
                                        DataRow comp = dscont.Tables["companies"].Rows.Find(contid);
                                        pulledcollection[i] = comp["company_name"].ToString();
                                        tb.Text = pulledcollection[i].ToString();
                                    } break;
                                case 1:
                                    {
                                        Control tb = (Control)currentcollection[i];
                                        int contid = (int)rc[r][5, DataRowVersion.Original];
                                        DataRow comp = dscont.Tables["companies"].Rows.Find(contid);
                                        currentcollection[i] = comp["company_name"].ToString();
                                        tb.Text = currentcollection[i].ToString();
                                        switch (typ)
                                        {
                                            case true:
                                                {
                                                    if (currentcollection[i].ToString() != pulledcollection[i].ToString() && i != 0 && i != 9 && i != 10)
                                                    { tb.BackColor = Color.PaleGreen; }
                                                } break;
                                            case false:
                                                {
                                                    if (currentcollection[i].ToString() != pulledcollection[i].ToString() && i != 0 && i != 10 && i != 11)
                                                    { tb.BackColor = Color.PaleGreen; }
                                                } break;
                                        }

                                    } break;
                                case 2:
                                    {
                                        Control tb = (Control)usercollection[i];
                                        int contid = (int)rc[r][5, DataRowVersion.Current];
                                        DataRow comp = dscont.Tables["companies"].Rows.Find(contid);
                                        usercollection[i] = comp["company_name"].ToString();
                                        tb.Text = usercollection[i].ToString();
                                        if (usercollection[i].ToString() != currentcollection[i].ToString() &&
                                            currentcollection[i].ToString() == pulledcollection[i].ToString())
                                        {
                                            switch (typ)
                                            {
                                                case true:
                                                    {
                                                        if (i != 0 && i != 9 && i != 10)
                                                        { tb.BackColor = Color.PaleGreen; }
                                                        nrb[contact[i]] = originalrow[contact[i], DataRowVersion.Current].ToString();
                                                        nrc[contact[i]] = originalrow[contact[i], DataRowVersion.Current].ToString();
                                                    } break;
                                                case false:
                                                    {
                                                        if (i != 0 && i != 10 && i != 11)
                                                        { tb.BackColor = Color.PaleGreen; }
                                                        nrb[company[i]] = originalrow[company[i], DataRowVersion.Proposed].ToString();
                                                        nrc[company[i]] = originalrow[company[i], DataRowVersion.Proposed].ToString();
                                                    } break;
                                            }
                                        }
                                        else if (usercollection[i].ToString() != currentcollection[i].ToString() && currentcollection[i].ToString() != pulledcollection[i].ToString()
                                            && usercollection[i].ToString() != pulledcollection[i].ToString())
                                        {

                                            switch (typ)
                                            {
                                                case true:
                                                    {
                                                        if (i != 0 && i != 9 && i != 10)
                                                        { tb.BackColor = Color.Yellow; }
                                                        nrc[contact[i]] = originalrow[contact[i], DataRowVersion.Current].ToString();
                                                    } break;
                                                case false:
                                                    {
                                                        if (i != 0 && i != 10 && i != 11)
                                                        { tb.BackColor = Color.Yellow; }
                                                        nrc[company[i]] = originalrow[company[i], DataRowVersion.Current].ToString();
                                                    } break;
                                            }
                                        }
                                    }
                                    break;
                            }
                        }
                        else if (i == 9 && typ == false)
                        {

                            switch (r)
                            {
                                case 0:
                                    {
                                        Control tb = (Control)pulledcollection[i];
                                        int catid = (int)rc[r][9, DataRowVersion.Original];
                                        DataRow cat = dscont.Tables["categories"].Rows.Find(catid);
                                        pulledcollection[i] = cat["category"].ToString();
                                        tb.Text = pulledcollection[i].ToString();
                                    } break;
                                case 1:
                                    {
                                        Control tb = (Control)currentcollection[i];
                                        int catid = (int)rc[r][9, DataRowVersion.Original];
                                        DataRow cat = dscont.Tables["categories"].Rows.Find(catid);
                                        currentcollection[i] = cat["category"].ToString();
                                        tb.Text = currentcollection[i].ToString();
                                        if (currentcollection[i].ToString() != pulledcollection[i].ToString())
                                        {
                                            switch (typ)
                                            {
                                                case true:
                                                    {
                                                        if (i != 0 && i != 9 && i != 10)
                                                        { tb.BackColor = Color.PaleGreen; }
                                                    } break;
                                                case false:
                                                    {
                                                        if (i != 0 && i != 10 && i != 11)
                                                        { tb.BackColor = Color.PaleGreen; }
                                                    } break;
                                            }
                                            tb.BackColor = Color.PaleGreen;
                                        }
                                    }
                                    break;
                                case 2:
                                    {
                                        Control tb = (Control)usercollection[i];
                                        int catid = (int)rc[r][9, DataRowVersion.Current];
                                        DataRow cat = dscont.Tables["categories"].Rows.Find(catid);
                                        usercollection[i] = cat["category"].ToString();
                                        tb.Text = usercollection[i].ToString();
                                        if (usercollection[i].ToString() != currentcollection[i].ToString() && currentcollection[i].ToString() == pulledcollection[i].ToString())
                                        {
                                            switch (typ)
                                            {
                                                case true:
                                                    {
                                                        if (i != 0 && i != 9 && i != 10)
                                                        { tb.BackColor = Color.PaleGreen; }
                                                        nrb[contact[i]] = originalrow[contact[i], DataRowVersion.Current].ToString();
                                                        nrc[contact[i]] = originalrow[contact[i], DataRowVersion.Current].ToString();
                                                    }
                                                    break;
                                                case false:
                                                    {
                                                        if (i != 0 && i != 10 && i != 11)
                                                        { tb.BackColor = Color.PaleGreen; }
                                                        nrb[company[i]] = originalrow[company[i], DataRowVersion.Current].ToString();
                                                        nrc[company[i]] = originalrow[company[i], DataRowVersion.Current].ToString();
                                                    }
                                                    break;
                                            }
                                        }
                                        else if (usercollection[i].ToString() != currentcollection[i].ToString() && currentcollection[i].ToString() != pulledcollection[i].ToString() && usercollection[i].ToString() != pulledcollection[i].ToString())
                                        {
                                            tb.BackColor = Color.Yellow;
                                            switch (typ)
                                            {
                                                case true:
                                                    {
                                                        if (i != 0 && i != 9 && i != 10)
                                                        { tb.BackColor = Color.Yellow; }
                                                        nrc[contact[i]] = originalrow[contact[i], DataRowVersion.Current].ToString();
                                                    } break;
                                                case false:
                                                    {
                                                        if (i != 0 && i != 10 && i != 11)
                                                        { tb.BackColor = Color.Yellow; }
                                                        nrc[company[i]] = originalrow[company[i], DataRowVersion.Current].ToString();
                                                    } break;
                                            }
                                        }
                                    }
                                    break;
                            }
                        }
                        else
                        {
                            switch (r)
                            {
                                case 0:
                                    {
                                        Control tb = (Control)pulledcollection[i];
                                        switch (typ)
                                        {
                                            case true:
                                                {
                                                    pulledcollection[i] = rc[r][contact[i], DataRowVersion.Original].ToString();
                                                    tb.Text = pulledcollection[i].ToString();
                                                } break;
                                            case false:
                                                {
                                                    pulledcollection[i] = rc[r][company[i], DataRowVersion.Original].ToString();
                                                    tb.Text = pulledcollection[i].ToString();
                                                } break;
                                        }
                                    }
                                    break;
                                case 1:
                                    {
                                        Control tb = (Control)currentcollection[i];
                                        switch (typ)
                                        {
                                            case true:
                                                {
                                                    currentcollection[i] = rc[r][contact[i], DataRowVersion.Original].ToString();
                                                    tb.Text = currentcollection[i].ToString();
                                                } break;
                                            case false:
                                                {
                                                    currentcollection[i] = rc[r][company[i], DataRowVersion.Original].ToString();
                                                    tb.Text = currentcollection[i].ToString();
                                                } break;
                                        }
                                        if (currentcollection[i].ToString() != pulledcollection[i].ToString())
                                        {
                                            switch (typ)
                                            {
                                                case true:
                                                    {
                                                        if (i != 0 && i != 9 && i != 10)
                                                        { tb.BackColor = Color.PaleGreen; }
                                                    } break;
                                                case false:
                                                    {
                                                        if (i != 0 && i != 10 && i != 11)
                                                        { tb.BackColor = Color.PaleGreen; }
                                                    } break;
                                            }
                                        }
                                    }
                                    break;
                                case 2:
                                    {
                                        Control tb = (Control)usercollection[i];
                                        switch (typ)
                                        {
                                            case true:
                                                {
                                                    usercollection[i] = rc[r][contact[i], DataRowVersion.Current].ToString();
                                                    tb.Text = usercollection[i].ToString();
                                                } break;
                                            case false:
                                                {
                                                    usercollection[i] = rc[r][company[i], DataRowVersion.Current].ToString();
                                                    tb.Text = usercollection[i].ToString();
                                                } break;
                                        }
                                        if (usercollection[i].ToString() != currentcollection[i].ToString() && currentcollection[i].ToString() == pulledcollection[i].ToString())
                                        {
                                            switch (typ)
                                            {
                                                case true:
                                                    {
                                                        if (i != 0 && i != 9 && i != 10)
                                                        { tb.BackColor = Color.PaleGreen; }
                                                        nrb[contact[i]] = originalrow[contact[i], DataRowVersion.Current].ToString();
                                                        nrc[contact[i]] = originalrow[contact[i], DataRowVersion.Current].ToString();
                                                    } break;
                                                case false:
                                                    {
                                                        if (i != 0 && i != 10 && i != 11)
                                                        { tb.BackColor = Color.PaleGreen; }
                                                        nrb[company[i]] = originalrow[company[i], DataRowVersion.Current].ToString();
                                                        nrc[company[i]] = originalrow[company[i], DataRowVersion.Current].ToString();
                                                    } break;
                                            }
                                            tb.BackColor = Color.PaleGreen;
                                        }
                                        else if (usercollection[i].ToString() != currentcollection[i].ToString() && currentcollection[i].ToString() != pulledcollection[i].ToString() && usercollection[i].ToString() != pulledcollection[i].ToString())
                                        {
                                            switch (typ)
                                            {
                                                case true:
                                                    {
                                                        if (i != 0 && i != 9 && i != 10)
                                                        { tb.BackColor = Color.Yellow; }
                                                        nrc[contact[i]] = originalrow[contact[i], DataRowVersion.Current].ToString();
                                                    } break;
                                                case false:
                                                    {
                                                        if (i != 0 && i != 10 && i != 11)
                                                        { tb.BackColor = Color.Yellow; }
                                                        nrc[company[i]] = originalrow[company[i], DataRowVersion.Current].ToString();
                                                    } break;
                                            }
                                        }
                                    }
                                    break;
                            }
                        }
                    }
                    if (r == 1)
                    {
                        chgtable.Rows.Add(nra);
                        chgtable.AcceptChanges();
                        chgrowa = chgtable.Rows[0];
                        func.fixdate = chgrowa;
                        chgtable.Rows.Add(nrb);
                        chgrowb = chgtable.Rows[1];
                        chgtable.Rows.Add(nrc);
                        chgrowc = chgtable.Rows[2];
                        chgtable.AcceptChanges();
                    }
                }

            }
            catch (Exception ex)
            {
                string subject = "Compare Form Concurreny Error";
                ErrorHandling.ErrorEmail(subject, ex);
            }
        }        

        void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            nm.Close();

            //progbar.Minimum = 1;
            progbar.Maximum = 30;
            //progbar.Value = 1;
            //progbar.Step = 1;
            timer2.Interval = 1000;
            timer2.Enabled = true;
            timer2.Tick += new EventHandler(timer2_Tick);           
            timer2.Start();
            
        }

        void timer2_Tick(object sender, EventArgs e)
        {
            if (progbar.Value != 30)
            {
                progbar.Value++;
            }
            else
            {
                timer2.Enabled = false;
                but_b.PerformClick();  
            }
        }

        public void but_a_Click(object sender, EventArgs e)
        {
            this.Close();
            //chgrowa = currentrow.Rows[0];
            func.fixdate = chgrowa;
            button = "a";
            func.button = button;
            Form c = forms["ContactsAddEdit"];
            c.Close();
        }

        public void but_b_Click(object sender, EventArgs e)
        {
            this.Close();
            //chgrowb = currentrow.Rows[0];
            func.update = chgrowb;
            button = "b";
            func.button = button;
            Form c = forms["ContactsAddEdit"];
            c.Close();
        }

        public void but_c_Click(object sender, EventArgs e)
        {
            this.Close();
            //chgrowc = currentrow.Rows[0];
            func.update = chgrowc;
            button = "c";
            func.button = button;
            Form c = forms["ContactsAddEdit"];
            c.Close();
        }

        public compare()
        {}

        private void compare_Load(object sender, EventArgs e)
        {}

        public DataTable temp
        { get; set; }        
    }
}
